<?php
/**
 * ログインモデルクラス<br />
 *
 * @package apamanshop
 * @author Y.Shibutani
 * @copyright
 */
class Login_Model_Login extends Apm_Model_Base
{
	/**
	 * アカウント情報の取得
	 *
	 * @param String $id ID
	 * @param String $password パスワード
	 * @return エラーコード
	 */
	public function getAccount($id,$password)
	{
		//DBパラメータ用配列生成
    	$db_params = array();

    	//=====パラメータ設定=====
        $db_params['eID'] = $id;
    	$db_params['ePass'] = $password;

        //=====SQL生成=====
		$strSQL  = "SELECT M92_USER_CD, M92_HANDLE_NAME FROM M92MYPAGE_USER ";
		$strSQL .= "WHERE M92_E_MAIL = :eID ";
		$strSQL .= "AND M92_PASSWORD = :ePass ";
		$strSQL .= "AND M92_DELETE_FLG = '0' ";

		//=====データ取得=====
		$userList = $this->dbGetList($strSQL, $db_params);
		return $userList;
	}

	public function InsertLog($ucd,$db)
	{
		$sql = "INSERT INTO W21PAGE_VIEW_LOG_MYPAGE ";
		$sql .= "(W21_DATE,W21_USER_ID) VALUES (SYSDATE, :ucd)";

		$db_params = array();
		$db_params['ucd'] = $ucd;
		$this->dbExecQuery($sql,$db_params,$db);
	}

	public function loginDate($ucd,$db)
	{
		$sql = "UPDATE M92MYPAGE_USER SET ";
		$sql .= "M92_PC_LAST_LOGIN_YMD = SYSDATE";
		$sql .= ",M92_PC_LOGIN_CNT = M92_PC_LOGIN_CNT + 1 ";
		$sql .= "WHERE ";
		$sql .= "M92_USER_CD = :ucd";

		$db_params = array();
		$db_params['ucd'] = $ucd;

		$this->dbExecQuery($sql,$db_params,$db);
	}

	/**
	 * お気に入り物件情報の登録
	 *
	 * @param $params
	 * @return エラーコード
	 */
	public function insertFavoriteBukken($params)
	{
		//DBパラメータ用配列生成
    	$db_params = array();

        $TorokuKbn = $params['TorokuKbn'];
        $resCd = "";

        //=====SQL生成=====
    //お気に入り物件登録(ﾏｲﾍﾟｰｼﾞ)
	If	($TorokuKbn == Apm_Config_Mypage::MYPAGE)
	{
        $sql = "";
        $sql .= "SELECT count(*) AS CNT FROM T99FAVORITE WHERE T99_USER_CD = '". $params['userCd'] . "' AND T99_HEYA_CD = '" . $params['heyaCd'] . "'";

        //=====データ取得=====
		$userDtl = $this->dbGetList($sql, $db_params);

		//重複チェック
		if ($userDtl[0]['CNT'] <> 0)
		  {
		  	$resCd = "9900";
		  }
		    else
		  {
		  	$sql = null;
		  	$sql .= "SELECT count(*) AS CNT FROM T99FAVORITE,T03WEB_HEYA WHERE T99_USER_CD = '". $params['userCd'] . "' AND T99_HEYA_CD = T03_HEYA_CD ";
		  	//=====データ取得=====
			$userDtl = $this->dbGetList($sql, $db_params);

			//登録件数チェック
			if ($userDtl[0]['CNT']>(Apm_Config_Mypage::CNT_MY_BUKKEN_MAX - 1))
			    {
			      $resCd = "9901";
			    }
			else
			    {
				  $db = $this->getDbConnection();
				  //トランザクション開始
				  $this->startTransaction($db);

			    try {
			    	$sql = "";
				  	$sql .= "INSERT INTO T99FAVORITE";
        		  	$sql .= "(";
		          	$sql .= "T99_USER_CD,";
		          	$sql .= "T99_HEYA_CD,";
		          	$sql .= "T99_TOUROKUBI,";
		          	$sql .= "T99_MYPAGE_KBN";
		          	$sql .= ")VALUES(";
		          	$sql .= "'" . $params['userCd'] . "',";
		          	$sql .= "'" . $params['heyaCd'] . "',";
		          	$sql .= "SYSDATE,";
		          	$sql .= "'1' )";

					//=====データ登録=====
					$this->dbExecQuery($sql, $db_params, $db,"T99FAVORITE");

					$this->commitTransaction($db);
					$resCd = "0000";

				} catch (Exception $e) {
					$this->rollbackTransaction($db);
					$resCd = "9902";
				}

				$db->closeConnection();

			    }
	  	  }
	}
	else
	{
        //お気に入り物件登録(ﾏｲﾍﾟｰｼﾞｸﾘｯﾌﾟ)
        $existFg = $params['ExistFg'];
        $listOver = $params['ListOver'];
		//重複時メッセージ表示用変数
		if ($existFg <> "" )
		{
			$resCd = "9900";
		//件数上限時メッセージ表示用変数
		}
		else If ($listOver <> "")
		{
			$resCd = "9901";
		}
		else
		{
			$resCd = "0000";
		}
	}
	return $resCd;
}
}
?>
